package cn.kgc.filter;

import javax.servlet.*;
import java.io.IOException;

public class CharacterFilter implements Filter {
    String charName=null;
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("初始化");
        charName=filterConfig.getInitParameter("charset");
    }

    /**
     * 執行過濾的操作
     * @param servletRequest
     * @param servletResponse
     * @param filterChain
     * @throws IOException
     * @throws ServletException
     */
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        servletRequest.setCharacterEncoding(charName);
        servletResponse.setCharacterEncoding(charName);
        System.out.println("进入字符集处理过滤器"+charName);
        filterChain.doFilter(servletRequest,servletResponse);
        System.out.println("退出字符集处理过滤器"+charName);
    }

    @Override
    public void destroy() {
        System.out.println("销毁");
    }
}
